package com.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.exception.LMSException;
import com.interfaces.EmployeeManager;
import com.interfaces.LeaveManager;
import com.manager.EmployeeManagerImpl;
import com.manager.LeaveManagerImpl;
import com.view.LeaveDetailView;
import com.view.PageDetailsView;

/**
 * Servlet implementation class LeaveApplyServlet.
 * 
 * @author : gbs02399 Created on : Jul 6, 2012 Project : LMS File Name :
 *         LeaveApplyServlet.java
 */
public class LeaveApplyServlet extends HttpServlet {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/**
	 * Instantiates a new leave apply servlet.
	 * 
	 * @see HttpServlet#HttpServlet()
	 */
	public LeaveApplyServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * Do get.
	 * 
	 * @param request
	 *            the request
	 * @param response
	 *            the response
	 * @throws ServletException
	 *             the servlet exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		doPost(request, response);
	}

	/**
	 * Do post.
	 * 
	 * @param request
	 *            the request
	 * @param response
	 *            the response
	 * @throws ServletException
	 *             the servlet exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		final String action = request.getParameter("MODO");
		final EmployeeManager employeeManager = new EmployeeManagerImpl();
		final LeaveManager leaveManager = new LeaveManagerImpl();
		System.out.println(action);
		if (action.equals("default")) {
			final String userId = request.getParameter("userid");
			System.out.println(userId);

			LeaveDetailView leaveDetailView;
			try {
				leaveDetailView = employeeManager
						.getEmployeeLeaveDetail(userId);
				request.setAttribute("leaveDetailView", leaveDetailView);
			} catch (final LMSException e) {
				e.printStackTrace();
				request.setAttribute("error", e.getMessage());
			}
			final PageDetailsView pageView = clearForm();
			request.setAttribute("pageView", pageView);
		}
		if (action.equals("formSubmit")) {
			final String userId = request.getParameter("userId");
			final String fromdate = request.getParameter("fromdate");
			final String toDate = request.getParameter("todate");
			final String leaveType = request.getParameter("leaveType");
			final String leaveReason = request.getParameter("leaveReason");

			final PageDetailsView pageView = new PageDetailsView();
			pageView.setFromDate(fromdate);
			pageView.setToDate(toDate);
			pageView.setLeaveType(leaveType);
			pageView.setLeaveReason(leaveReason);
			LeaveDetailView leaveDetailView = null;
			try {
				leaveDetailView = employeeManager
						.getEmployeeLeaveDetail(userId);
				leaveManager.applyLeave(pageView,leaveDetailView);
				final PageDetailsView emptyPageView = clearForm();
				leaveDetailView = employeeManager
						.getEmployeeLeaveDetail(userId);
				request.setAttribute("pageView", emptyPageView);
				request.setAttribute("leaveDetailView", leaveDetailView);
				request.setAttribute("Message", "Leave applied successflly");
			} catch (final LMSException e) {
				e.printStackTrace();
				request.setAttribute("pageView", pageView);
				request.setAttribute("leaveDetailView", leaveDetailView);
				request.setAttribute("error", e.getMessage());
			}
		}
		final RequestDispatcher reqDispatch = getServletContext()
				.getRequestDispatcher("/LeaveDetails.jsp");
		reqDispatch.forward(request, response);
	}

	/**
	 * Clear form.
	 * 
	 * @return the page details view
	 */
	private PageDetailsView clearForm() {
		final PageDetailsView pageView = new PageDetailsView();
		pageView.setFromDate("");
		pageView.setToDate("");
		pageView.setLeaveType("");
		pageView.setLeaveReason("");
		return pageView;
	}

}
